package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Categoria;
import com.liwalo.entities.Pais;
import com.liwalo.entities.Subcategoria;
import com.liwalo.entities.TiendaProducto;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Categoria.
 * @see com.liwalo.dao.Categoria
 * @author Hibernate Tools
 */
public class CategoriaDAO {

	private static final Log log = LogFactory.getLog(CategoriaDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public void persist(Categoria transientInstance) {
		log.debug("persisting Categoria instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Categoria instance) {
		log.debug("attaching dirty Categoria instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Categoria instance) {
		log.debug("attaching clean Categoria instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Categoria persistentInstance) {
		log.debug("deleting Categoria instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Categoria merge(Categoria detachedInstance) {
		log.debug("merging Categoria instance");
		try {
			Categoria result = (Categoria) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Categoria findById(java.lang.Integer id) {
		log.debug("getting Categoria instance with id: " + id);
		try {
			Categoria instance = (Categoria) sessionFactory.getCurrentSession()
					.get("com.liwalo.entities.Categoria", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Categoria> findByExample(Categoria instance) {
		log.debug("finding Categoria instance by example");
		try {
			List<Categoria> results = (List<Categoria>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.entities.Categoria")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Categoria> getAllValues() {
		try {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Categoria.class)
				.add(Restrictions.eq("estado",1) );
		return (List<Categoria>) criteria.list();
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Categoria> listadoCategoriasTienda(TiendaProducto tiendaprovo) {
		try {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Categoria.class,"ca").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.createCriteria("ca.tiendaProductoList","tiendaproductoList")
				.createCriteria("ca.subcategoriaList","subcategorialist")
				.add(Restrictions.eq("tiendaproductoList.tiendaId",tiendaprovo.getTiendaId()))
				.add(Restrictions.eq("ca.estado",1)
				
						
						);
		return (List<Categoria>) criteria.list();
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	
	
	
	
}
